from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth.hashers import check_password
from django.core.paginator import Paginator
from .serializer import *
from django.db.models import Q


# 登录
class LoginView(APIView):
    def post(self,request):
        # 接收参数
        uername=request.data.get('username','')
        password=request.data.get('password','')

        userobj=models.User.objects.filter(username=uername).first()
        if not userobj:
            return Response({'code':1001,'msg':'用户名不存在'})
        pwd=userobj.password
        if check_password(password,pwd):
            data={
                'uid':userobj.id,
                'auth':userobj.auth
            }
            return Response({'code': 1000, 'msg': '登录成功','data':data})
        return Response({'code': 1002, 'msg': '用户名或密码错误'})


# 员工
class UserView(APIView):
    # 添加
    def post(self,request):
        # 接收参数
        username = request.data.get('username','').strip()
        password = request.data.get('password','').strip()
        depart = request.data.get('depart',0)

        # 重复性
        userobj=models.User.objects.filter(username=username).first()
        if userobj:
            return Response({'code':1001,'msg':'数据已存在'})

        # 构造数据

        data={
            'username':username,
            'password':password,
            'depart':depart
        }

        # 反序列化
        obj = UserSerializer(data=data)

        # 判断
        if obj.is_valid():
            obj.save()
            return Response({'code': 1000, 'msg': '添加成功'})
        # 打印错误信息
        print(obj.errors)

        # 返回
        return Response({'code': 1002, 'msg': '参数有误'})


    # 查询
    def get(self,request):
        # 接收参数
        p=request.GET.get('p',1)
        keyword=request.GET.get('keyword','').strip()
        # 查询所有数据
        userqueryset=models.User.objects.filter(Q(username__contains=keyword)|Q(depart__name__contains=keyword)).all()
        # 实例化分页
        page_obj=Paginator(userqueryset,per_page=2)
        # 获取分页的范围
        page=[i for i in page_obj.page_range]
        # 获取当前页的数据
        page_data=page_obj.page(p)
        # 判断上一页和下一页
        previous=page_data.has_previous()
        next=page_data.has_next()
        # 序列化当前页的内容
        obj=UserSerializer(page_data,many=True)
        # 构造数据
        data={
            'data':obj.data,
            'page':page,
            'previous':previous,
            'next':next
        }

        # 返回
        return Response({'code':1000,'msg':'','data':data})


# 部门
class DepartmentView(APIView):
    # 添加
    def post(self,request):
        # 接收参数
        name=request.data.get('name','').strip()

        # 重复性
        deptobj=models.Department.objects.filter(name=name).first()
        if deptobj:
            return Response({'code':1001,'msg':'数据已存在'})

        # 构造数据

        data={
            'name':name
        }

        # 反序列化
        obj = DepartmentSerializer(data=data)

        # 判断
        if obj.is_valid():
            obj.save()
            return Response({'code': 1000, 'msg': '添加成功'})
        # 打印错误信息
        print(obj.errors)

        # 返回
        return Response({'code': 1002, 'msg': '参数有误'})


    # 查询
    def get(self,request):
        deptqueryset=models.Department.objects.all()
        obj=DepartmentSerializer(deptqueryset,many=True)
        return Response({'code':1000,'msg':'','data':obj.data})
